/**********************************************************
** Replication file for Ban, Palmer and Schneer 2019     **
** file: analyze_rd.do                                   **
** date: 5/29/2019                                       **
**********************************************************/


clear all
set more off

*set working directory
cd "/nfs/home/B/bschneer/shared_space/bschneer/lobbying_replication"

*load relevant data

*net install rdrobust, from(https://sites.google.com/site/rdpackages/rdrobust/stata) replace
*ssc install outreg2
*ssc install scheme_tufte

*RD Criteria
local restrict1 "first_race"
local restrict2 "cand_pairs"
local lrestrict1 "First Race"
local lrestrict2 "First-time Candidate Pairs"

*RD Implementation

*Load data
use usr_gen/candidates, clear

**Select whether performing estimates for House or for Senate
**by commenting/uncommenting below

*local chamber = "H"
local chamber = "S"

keep if office == "`chamber'"

*label variables
label var amount "Lobbyist Total Revenues ({c $|})"
label var amount_per_lobbyist "Lobbyist Revenues per Person ({c $|})"
label var amount_weighted_lim "Lobbyist Weighted Revenues ({c $|})"
label var fe_lim "Lobbyist Value Added, Revenues Per Client/Year ({c $|})"
label var fe "Lobbyist Value Added, Revenues Per Client/Year (Negative Values Allowed) ({c $|})"
label var amount_cy "Lobbyist Revenues per Client/Year ({c $|})"
label var amount_per_lobbyist_cy "Lobbyist Revenues per Person per Client/Year ({c $|})"
label var amount_yrly "Lobbyist Total Revenues per Year ({c $|})"
label var amount_weighted_lim_yrly "Lobbyist Weighted Revenues per Year ({c $|})"
label var amount_per_lobbyist_yrly "Lobbyist Revenues per Person per Year ({c $|})"

*Fill outcome variables for those who did not become lobbyist
replace became_lobbyist = 0 if became_lobbyist==.

*Create additional outcome variables
gen ln_amount_weighted_lim = log(1+ amount_weighted_lim)
gen ln_amount_per_lobbyist = log(1+amount_per_lobbyist)

*label them
label var amount_per_lobbyist "log(Revenues per Person)"
label var amount_weighted_lim "log(LVA Weighted Revenues)"

*Save data for analysis
tempfile data
save `data', replace


*Create summary statistics table 

gen n = 1
collapse (sum) n (mean) amount_weighted_lim_yrly, by(became_lobbyist everwon)
reshape wide amount_weighted_lim_yrly n, i(everwon) j(became_lobbyist)
drop amount_weighted_lim_yrly0

gen tot = n0 + n1

drop n0

gen n1pct = n1/tot

order everwon tot n1 n1pct amount_weighted_lim_yrly1

export delimited using "out/tablea1_`chamber'.csv", replace


****Implement RD

use `data', clear

* ID Rainmakers
_pctile amount_weighted_lim_yrly, p(90)

gen rainmaker = (amount_weighted_lim_yrly>`r(r1)' & amount_weighted_lim_yrly < .)
replace lobbyist = upper(lobbyist)

***** we use rdrobust estimation procedure

*state FEs
egen state_n = group(state)

*Outcome variable labels
local lab1 "Became a Lobbyist"
local lab2 "Lobbyist Revenues per Person per Year"
local lab3 "Lobbyist Weighted Revenues per Year"
local lab4 "Lobbyist Revenues per Person"
local lab5 "Lobbyist Weighted Revenues"
local lab6 "Rainmakers"
local lab7 "log(LVA Weighted Revenues)"
local lab8 "log(Amount Per Lobbyist)"

*Loop across different outcomes and estimate RD
local k=1

foreach var of varlist became_lobbyist amount_per_lobbyist_yrly amount_weighted_lim_yrly amount_per_lobbyist amount_weighted_lim rainmaker ln_amount_weighted_lim ln_amount_per_lobbyist{

*We create Plots in R so as to be able to include confidence intervals
*Old versions commented out below
//rdplot `var' vote_margin if abs(vote_margin)<=.2,  p(1) nbins(20) graph_options(title("") xtitle("Vote Margin") ytitle("`lab`k''") scheme("tufte") legend(off))
//graph export "out/rd_`var'_`chamber'.pdf", replace

*loop over different bandwidths
foreach h in 0.05 0.075 0.1{

rdrobust `var' vote_margin, p(1) fuzzy(everwon) h(`h') vce(cluster state_n) kernel(epa)
local bw = (e(h_l) + e(h_r))/2
local obs = e(N_h_l) + e(N_h_r)

*output to tables
outreg2 using "out/rd_out_`var'_`chamber'",  title("Effect of Holding Office on Lobbying, `lab`k''") ctitle(" ") cttop("`chamber'") addstat(BW Loc. Poly., `bw') addtext(Observations, `obs') tex(frag) label noobs append nonotes addnote("SEs clustered at state level. *** p<0.01, ** p<0.05, * p<0.1")
}
local k = `k'+1
}

*save outcome data for plots to be made in R
save usr_gen/candidates_`chamber'_for_rdd_plots, replace



*Robustness checks for RD

*Create additional variables
gen female = gender=="F"
gen northeast = census_region == "Northeast"
gen west = census_region=="West"
gen south = census_region == "South"
gen midwest = census_region == "Midwest"
gen dem = party=="D"

*label them
label var female "Female"
label var northeast "Northeast"
label var west "West"
label var south "South"
label var midwest "Midwest"
label var dem "Democratic"

*Loop over placebo outcomes
local k=1

foreach var of varlist female northeast south midwest west dem {

local lab: variable label `var'

rdrobust `var' vote_margin, p(1) fuzzy(everwon) h(.05) vce(cluster state_n) kernel(epa)
local bw = (e(h_l) + e(h_r))/2
local obs = e(N_h_l) + e(N_h_r)
*output to table
outreg2 using "out/rd_out_robustness_`chamber'",  title("Robustness Checks: Effect of Holding Office on Pre-Treatment Covariates") ctitle("`lab'") cttop("`chamber'") addstat(BW Loc. Poly., .05) addtext(Observations, `obs') tex(frag) label noobs append nonotes addnote("SEs clustered at state level. *** p<0.01, ** p<0.05, * p<0.1")
local k = `k'+1
}


*Comparable age robustness check (Only for Senate)

if "`chamber'" == "S" {
*load data
use inputs/senate_age, clear

*merge age data
rename name cand_name
split cand_name, gen(name) parse(", ")
rename name1 last
drop name* 

rename w_g winner

tempfile temp

save `temp', replace

use `data', clear
duplicates drop
split cand_name, gen(name) parse(", ")
rename name1 last
drop name*

merge 1:1 last state year winner using `temp'
tempfile temp2 tempm1 tempm2

save `temp2', replace

keep if _merge==2
drop _merge
save `tempm2', replace

use `temp2', clear
keep if _merge==1
drop _merge
drop age
merge 1:1 last state winner using `tempm2'
keep if _merge==3
drop _merge

append using `temp2'

keep if _merge ==3 | _merge==.
drop _merge

*Generate age variable at end of sample
gen age_end = 2014-year+age

*restrict sample
keep if age>=60 & age<=70

*Gen state FEs
egen state_n = group(state)

*loop over outcomes
local k=1

foreach var of varlist became_lobbyist ln_amount_weighted_lim ln_amount_per_lobbyist {

foreach h in 0.05 0.075 0.1{

rdrobust `var' vote_margin, p(1) fuzzy(everwon) h(`h') vce(cluster state_n) kernel(epa)
local bw = (e(h_l) + e(h_r))/2
local obs = e(N_h_l) + e(N_h_r)
outreg2 using "out/rd_out_age_`var'_S",  title("Robustness Check: Effect of Holding Office on Lobbying, Restricted Age (60-70 in 2014)") ctitle(" ") cttop("`chamber'") addstat(BW Loc. Poly., `bw') addtext(Observations, `obs') tex(frag) label noobs append nonotes addnote("SEs clustered at state level. *** p<0.01, ** p<0.05, * p<0.1")
}
local k = `k'+1
}

}
